<?php

namespace app\common\business;
use app\common\model\mysql\Category as CategoryModrl;
use think\facade\Log;

class Category
{
    public $categoryObj = null;
    public function __construct()
    {
        $this->categoryObj = new CategoryModrl();
    }

    public function add($data)
    {
        $data['status'] = config("status.mysql.table_normal");
        //$name = $data['name'];

        try {
            $this->categoryObj->save($data);
        }catch (\Exception $e){
            throw new \think\Exception("服务内部异常");
        }
        return $this->categoryObj->getLastInsID();
    }


    public function getNormalCategorys()
    {
        $field = "id,name,pid";
        $categorys = $this->categoryObj->getNormalCategorys($field);
        if(!$categorys){
            return $categorys;
        }
        $categorys = $categorys->toArray();
        return $categorys;
    }


    public function getLists($data,$num)
    {
        $list = $this->categoryObj->getLists($data,$num);
        if(!$list){
            return  [];
        }

        $result = $list->toArray();
        $result['render'] = $list->render();

        $pids = array_column($result['data'],"id");

        if($pids){
            $idCountResult = $this->categoryObj->getChildCountInPids($pids);
//            halt($idCountResult);

            $idCountResult = $idCountResult->toArray();

            $idCounts = [];
            foreach ($idCountResult as $countResult){
                $idCounts[$countResult['pid']] = $countResult['count'];
            }
        }


        if($result['data']){
            foreach ($result['data'] as $k => $value) {
                $result['data'][$k]['childCount'] = $idCounts[$value['id']] ?? 0;
            }
        }

        return $result;
    }

    /**
     * 根据ID获取 某一条记录
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getById($id)
    {
        $result = $this->categoryObj->find($id);
        if(empty($result)){
            return [];
        }

        $result = $result->toArray();
        return $result;
    }


    /**
     * 排序
     * @return void
     */
    public function listorder($id,$listorder)
    {
        $res = $this->getById($id);
        if(!$res){
            throw new \think\Exception("不存在该条记录");
        }

        $data = [
            'listorder' => $listorder
        ];

        try {
            $res = $this->categoryObj->updateById($id,$data);
        }catch (\Exception $e){
            Log::error("更新失败{$id}");
            return false;
        }

        return $res;
    }


    /**
     * 更新状态
     * @param $id
     * @param $status
     * @return false|null
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function status($id,$status)
    {
        $res = $this->getById($id);
        if(!$res){
            throw new \think\Exception("不存在该条记录");
        }

        if($res['status'] == $status){
            throw new \think\Exception("修改前和修改后一样没有任何意义");
        }

        $data = [
            "status" => intval($status)
        ];

        try {
            $res = $this->categoryObj->updateById($id,$data);
        }catch (\Exception $e){
            //记得记录日志
            return false;
        }

        return $res;
    }


}